package com.bw.yk04;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import ru.yandex.clickhouse.ClickHouseDriver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class ClickhouseSink extends RichSinkFunction<TM6Entity> {
    Connection conn = null;
    PreparedStatement ps = null;
    @Override
    public void open(Configuration parameters) throws Exception {
        Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
        conn = DriverManager.getConnection("jdbc:clickhouse://hadoop-single:8123", "default", "");
//        conn.setAutoCommit(true);
        ps = conn.prepareStatement("insert into dws_click_page_window values(?,?,?,?,?,?,?,?)");
    }

    @Override
    public void close() throws Exception {
        ps.close();
        conn.close();
    }

    @Override
    public void invoke(TM6Entity value, Context context) throws Exception {
        ps.setString(1,value.getStt());
        ps.setString(2,value.getEdt());
        ps.setString(3,value.getAr());
        ps.setString(4,value.getCh());
        ps.setString(5,value.getBa());
        ps.setLong(6,value.getPv());
        ps.setLong(7,value.getUv());
        ps.setLong(8,value.getDuringTime());
        ps.execute();
    }
}
